package com.ef.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.ClassType;
import com.ef.service.ClassTypeService;
import com.ef.util.CustomExample;

public class ClassTypeAction extends BaseAction<ClassType> {

	private static final long serialVersionUID = 1L;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ClassLevelAction.class);
	
	private ClassTypeService classTypeService;
	
	
	@SuppressWarnings("serial")
	public String list() {

		this.listResult = classTypeService.findByExample(new CustomExample<ClassType>(this.getModel()){
			
			public void appendCondition(Criteria criteria) {
				criteria.addOrder(Order.asc("description"));
				this.enableLike(MatchMode.ANYWHERE);
			}
		}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {
		
		return EDIT;
	}
	
	public String save(){
		
		ClassType classType =this.getModel();
		classType.setUpdateTime(new Date());
		classType.setUpdateUser(getSessionUser());
		classTypeService.saveOrUpdate(classType);
		this.setResult(ConfirmMessage.ClassType);
		return EDIT;
	}
	
	public String delete(){
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for(String i:id){
			classTypeService.delete(classTypeService.findById(Long.valueOf(i)));
		}
	 
		return DELETE;
	}
	public String same() throws IOException{
		String same = getRequest().getParameter("same");
		List<ClassType> list = classTypeService.findByName(same);
		String result = "{sucess:true,same:false}";
		if(list==null || list.size() == 0){
			
		}else{
			result = "{sucess:true,same:true}";
		}
		getResponse().getWriter().write(result);
		return NONE;
	}
	public ClassTypeService getClassTypeService() {
		return classTypeService;
	}

	public void setClassTypeService(ClassTypeService classTypeService) {
		this.classTypeService = classTypeService;
	}
	
	public String autoCompleteAjax(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String q=request.getParameter("q");
		String limit=request.getParameter("limit");
		List<ClassType> list= classTypeService.findByName(q,Integer.valueOf(limit));
		//[{id:id;name:name},{id:id;name:name}]
		String  responseStr="";;
 		//System.out.println(responseStr);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/plain;charset=GBK ");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			for (ClassType key:list){
				responseStr=("{id:")+(key.getId())+(",")+("name:\"")+(key.getName())+("\"}");
				//System.out.println(responseStr);
				out.println(responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {

				}
			}
		}
		return null;
	}
	
}
